﻿using System;
using System.Reflection;
using MugenInjection.Interception.Interface;
using MugenInjection.Interface;

// ReSharper disable CheckNamespace

namespace MugenInjection.Parameters
// ReSharper restore CheckNamespace
{
    internal sealed class ProxyCollectionParameter : IInjectionParameter
    {
        #region Fields

        private readonly IProxyCollection _proxyCollection;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ProxyCollectionParameter" /> class.
        /// </summary>
        public ProxyCollectionParameter(string name, IProxyCollection proxyCollection)
        {
            _proxyCollection = proxyCollection;
            MemberType = MemberTypes.Constructor;
            Name = name;
            ParameterType = typeof (IProxyCollection);
        }

        #endregion

        #region Overrides of InjectionParameterBase

        /// <summary>
        /// Gets the specified parameter name.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the specified parameter <see cref="Type"/>.
        /// </summary>
        public Type ParameterType { get; private set; }

        /// <summary>
        /// Gets the specified parameter <see cref="MemberTypes"/>.
        /// </summary>
        public MemberTypes MemberType { get; private set; }

        /// <summary>
        /// Determines whether the specified request can be resolved.
        /// </summary>
        /// <param name="memberInfo">The specified <see cref="MemberInfo"/></param>
        /// <param name="parameter">The specified <see cref="ParameterInfo"/> </param>
        /// <returns><c>True</c> if the specified request has been resolved; otherwise, <c>false</c>.</returns>
        public bool CanResolve(MemberInfo memberInfo, ParameterInfo parameter)
        {
            return parameter != null && Name.Equals(parameter.Name);
        }

        /// <summary>
        /// Gets parameter value for specified <see cref="IParameterContext"/>.
        /// </summary>
        /// <param name="parameterContext">The specified <see cref="IParameterContext"/>.</param>
        /// <returns>An instance of the parameter value.</returns>
        public object GetValue(IParameterContext parameterContext)
        {
            return _proxyCollection;
        }

        #endregion

        #region Implementation of IEquatable<IInjectionParameter>

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IInjectionParameter other)
        {
            return ReferenceEquals(this, other);
        }

        #endregion
    }
}